package multiThread;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * <b> 描述:</b> ;<br/>
 *
 * @author <b>作者</b>cdxuhongya@jd.com
 *         <b>时间</b>2017/3/13 14:39<br/>
 *         <b>@CopyRight (c) </b>2015-京东智能版权所有<br/>
 */
public class CallableLearn {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        List<Future<Integer>> futureList = new ArrayList<Future<Integer>>();

        for(int i = 0;i < 100; i ++){
            futureList.add(executorService.submit(new CallableTest(i)));
        }


        for (Future<Integer> fs : futureList){
            try {
                System.out.println(fs.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

    }




}


/**
 * callable通过call返回任务执行结果，调用Future的get方法，当任务没有执行
 * 完成，将会阻塞，可以通过isDone进行判断任务是否执行完成
 *
 * */
class CallableTest implements Callable<Integer> {

    public CallableTest(Integer id){
        this.id = id;
    }

    private Integer id;

    public Integer call()throws Exception{
        Thread.sleep(new Random(System.currentTimeMillis()).nextInt(10000));
        System.out.println("calculating--------------" + id);
        return id;
    }
}
